var dataContent = null;
var delUserAppURL="/profile/delUserApp/"
$(function () {
    dataContent = new Vue({
        el: '#projectdataTable',
        data: {
            userProjects: []
        },
        methods: {
            updateUserProject:function(id) {
                updateUserProject(id)
            },
            delUserApp:function(id){
                delUserApp(id);
            }
        },
        created: function () {
            var _this = this;
            $.getJSON("/profile/settingData", {}, function (resp) {
                if (resp.code != "0000") {
                    showMessage(resp.code || resp.desc, "error");
                } else {
                    _this.userProjects = resp.data;
                }
            })
        }
    });
})

function updateUserProject(id){
    loadDialog("#AllModal", "修改工程追踪", "/profile/updateUserApp/"+id+".html", function () {
        $("#updateUserProjectForm").ajaxForm({
            target: '#btn-userApp-update',   // target element(s) to be updated with server response
            dataType: 'json',
            url: "/profile/updateUserApp",
            beforeSubmit: function (formData, jqForm, options) {
                return true;
            },
            success: function (result, statusText, xhr, $form) {
                if (result.code != "0000") {
                    showMessage(result.code || result.desc, "error");
                } else {
                    $("#AllModal").modal("hide");
                    for(var i=0;i<dataContent.userProjects.length;i++){
                        if(dataContent.userProjects[i].appId == id){
                            dataContent.userProjects[i].userVersion=$("#userVersion").val();
                            break;
                        }
                    }
                }
            }
        });
    });
}

function addUserProject() {
    loadDialog("#AllModal", "新增工程追踪", "/profile/addUserProject.html", function () {
        var userProjectVUE = new Vue({
            el: '#createUserProjectForm',
            data: {
                projects: [],
                curProject: {},
                versions: [0]
            },
            methods: {
            },
            created: function () {
                var _this = this;
                $.getJSON("/profile/projects", {}, function (resp) {
                    if (resp.code != "0000") {
                        showMessage(resp.code || resp.desc, "error");
                    } else {
                        _this.projects = resp.data;
                    }
                })
            }
        });
        $("#appId").change(function () {
            for (var i = 0; i < userProjectVUE.projects.length; i++) {
                if (userProjectVUE.projects[i].appId == userProjectVUE.curProject.appId) {
                    userProjectVUE.curProject = userProjectVUE.projects[i];
                    break;
                }
            }
        });

        $("#createUserProjectForm").ajaxForm({
            target: '#btn-userApp-submit',   // target element(s) to be updated with server response
            dataType: 'json',
            url: "/profile/saveUserApp",
            beforeSubmit: function (formData, jqForm, options) {
                return true;
            },
            success: function (result, statusText, xhr, $form) {
                if (result.code != "0000") {
                    showMessage(result.code || result.desc, "error");
                } else {
                    $("#AllModal").modal("hide");
                    dataContent.userProjects.push(result.data);
                }
            }
        });


    })
}
function delUserApp(id){
    var deleteDialog = $.confirm({
        type: 'red',
        animationSpeed: 300,
        title: false,
        content: '确认删除该记录吗？',
        buttons: {
            confirm: {
                text: '确认',
                btnClass: 'waves-effect waves-button',
                action: function () {
                    $.ajax({
                        type: 'post',
                        url: delUserAppURL + id,
                        success: function (result) {
                            if (result.code != "0000") {
                                showMessage(result.code || result.desc, "error");
                            } else {
                                deleteDialog.close();
                                showMessage("删除成功");
                                for(var i=0;i<dataContent.userProjects.length;i++){
                                    if(dataContent.userProjects[i].appId == id){
                                        dataContent.userProjects.splice(i,1)
                                        break;
                                    }
                                }
                            }
                        }
                    });
                }
            },
            cancel: {
                text: '取消',
                btnClass: 'waves-effect waves-button'
            }
        }
    });
}
function loadDialog(selectEL, title, loadURI, success) {
    $(selectEL).find(".modal-title").html(title);
    $(selectEL).find('.modal-body').load(loadURI, function (response, status, xhr) {
        $(selectEL).modal("show");
        success();
    });
}